package com.itheima.threadtest3;

public class MyRunnable implements Runnable {

    // Runnable对象只创建一次 不用加 static
    static int count = 0;

    static Object lock = new Object();

    public void run() {
        while (true) {
            synchronized (lock) {
                if (count > 100) {
                    System.out.println("输出完毕");
                    break;
                } else {
                    // 输出奇数并递增 count
                    if (count % 2 == 1) {
                        System.out.println(Thread.currentThread().getName() + ": " + count);
                    }
                    count++;
                }
            }
            try {
                Thread.sleep(100); // 模拟执行时间
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
